package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.PageQuery;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.UserService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据id查询用户和用户地址信息
     *
     * @param id 用户id
     * @return 用户对象
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户和用户地址信息")
    public UserVO queryUserById(@ApiParam(value = "用户id", required = true) @PathVariable Long id) {
//        return userService.queryUserById(id);
//        User user = userService.getById(id);

        return userService.getUserAndAddressById(id);
    }

    /**
     * 根据用户id集合查询用户
     *
     * @param ids 用户id集合
     * @return 用户集合
     */
    @GetMapping()
    @ApiOperation("根据用户id集合查询用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "用户id集合", required = true, dataType = "List<Long>")
    )
    public List<UserVO> queryUserByIds(@RequestParam List<Long> ids) {
//        return userService.queryUserByIds(ids);
//        List<User> users = userService.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class, CopyOptions.create().setIgnoreNullValue(true));

        return userService.getUserAndAddressByIds(ids);
    }

    /**
     * 新增用户
     *
     * @param userFormDTO 用户Form传递对象
     * @return 新增成功的字符串
     */
    @PostMapping
    @ApiOperation("新增用户")
    public String saveUser(@RequestBody UserFormDTO userFormDTO) {
//        userService.saveUser(user);
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
        return "新增成功";
    }

    /**
     * 根据id删除用户
     *
     * @param id 用户id
     * @return 删除成功的字符串
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除用户")
    public String deleteUser(@ApiParam("用户id") @PathVariable Long id) {
//        userService.deleteUser(id);
        userService.removeById(id);
        return "删除成功";
    }

    /**
     * 修改用户
     *
     * @param userFormDTO 用户Form传递对象
     * @return 修改成功的字符串
     */
    @PutMapping
    @ApiOperation("修改用户")
    public String updateUser(@ApiParam(value = "用户Form传递对象", required = true) @RequestBody UserFormDTO userFormDTO) {
        User user = new User();
        BeanUtil.copyProperties(userFormDTO, user, CopyOptions.create().setIgnoreNullValue(true));
        userService.updateById(user);
        return "修改成功";
    }

    @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("根据id和余额修改用户")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long"),
                    @ApiImplicitParam(name = "money", value = "余额", required = true, dataType = "Integer")}
    )
    public String updateUserByIdAndBalance(@PathVariable Long id, @PathVariable Integer money) {

        userService.updateUserByIdAndBalance(id, money);
        return "修改成功";
    }

    /**
     * 根据复杂条件查询用户
     *
     * @param userQuery 用户查询对象
     * @return 用户对象
     */
    @GetMapping("/list")
    @ApiOperation("根据复杂条件查询用户")
    public List<UserVO> queryUserList(@ApiParam(value = "用户查询对象", required = true) UserQuery userQuery) {
        List<User> users = userService.queryUserList(userQuery);
        return BeanUtil.copyToList(users, UserVO.class);
    }

    @GetMapping("/page")
    @ApiOperation("根据条件分页查询用户")
    public PageDTO<UserVO> queryUserPage(@ApiParam(value = "分页查询对象", required = true) PageQuery pageQuery) {

        return userService.queryUserPage(pageQuery);
    }

}
